//
//  LXAlertInViewController.m
//  bgms_d
//
//  Created by Mountain on 4/3/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXAlertInViewController.h"
#import "LXDevSettingViewController.h"
#import "LXDevSettingModel.h"

@interface LXAlertInViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation LXAlertInViewController

#pragma mark -  public method

#pragma mark -  life cycle
- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initializationData];
    [self addNotification];
    
    [self loadUI];
    
    [self loadViewDatas];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated
{
    
}

- (void)viewWillDisappear:(BOOL)animated
{

}
#pragma mark -  over write

#pragma mark -  setter & getter

#pragma mark -  private method
- (void)initializationData
{
    self.selectIndex = 0;
    if (![self.selectString isEqualToString:@""]) {
        [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = obj;
            if ([dic[@"name"] isEqualToString:self.selectString]) {
                self.selectIndex = idx;
            }
        }];
    }
}

- (void)loadUI
{
    self.view.backgroundColor = kAppBackgroundColor;
    [self buildNavi];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)loadViewDatas
{
    
}

- (void)buildNavi
{
    
}

#pragma mark -  notification
- (void)addNotification
{
    
}

#pragma mark -  network


#pragma mark -  event handle
- (void)deleteDown:(UIButton *)sender
{
    
}

- (void)switchDown:(UISwitch *)sender
{
    
}
#pragma mark -  delegate
#pragma mark
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LXAutoLength lx_L:52];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = dic[@"name"];
    cell.textLabel.textColor = kAppNormalTextColor;
    [cell drawAroundViewWithLocation:DrawBottom color:kAppLinesColor insets:UIEdgeInsetsMake(0, 0, 0.5, 0)];
    UIImageView *showImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 18, 16, 16)];
    showImage.image = [UIImage imageNamed:@"okIcon"];
    cell.accessoryView = showImage;
    if (self.selectIndex == indexPath.row) {
        cell.accessoryView.hidden = NO;
    } else {
        cell.accessoryView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
        if (indexPath.row == idx) {
            cell.accessoryView.hidden = NO;
        } else {
            cell.accessoryView.hidden = YES;
        }
    }];
    [LXDevSettingModel shareSettingModel].alertInterval = [self.dataArray objectAtIndex:indexPath.row][@"name"];
}

#pragma mark - LXSwitchViewDelegate

#pragma mark -  lazy load
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < 12; i ++) {
            NSDictionary *dic = @{@"selected" : @"NO",
                                  @"name" : [NSString stringWithFormat:@"%ld %@", (i + 1) * 5, NSLocalizedString(@"Minute", nil)]};
            [_dataArray addObject:dic];
        }
    }
    return _dataArray;
}

#pragma mark -  memoryWarning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
